/*-------------------<-- Start of Description -->--------------------\ | Generate random variate from a binomial distribution | |-----------------------------------------| |--------------------------------------------------------------------| |-------------------------| | Arguments Need: | | seed - seed; Required, default is the current system time; | | var - the variable to save the generated random variates; | | n - the sample size; | | p - the sample proportion; | | temp - the temporary variable to update the seed; | | default is _ranbin0_; | |---------------------------| |--------------------------------------------------------------------| |---------------------------------| |Example | | data one; | | do i=1 to 20; | | x=%_ranbin(n=10, p=0.5); | | %_ranbin(seed=12345,var=y, n=20, p=0.5); | | output; | | end; | | run; %print(one); | |Usage: %_ranbin(seed=%sysfunc(datetime(), 15.), var=, n=REQUIRED, | | p=REQUIRED, temp=_ranbin0_); | \----------------------------------*/ %macro _ranbin(seed=%sysfunc(datetime(), 15.), var=, n=REQUIRED, p=REQUIRED, temp=_ranbin0_); /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 3-22-2002 6:30pm; | | Purpose: Random Binomial Generator; | \--------------------------------------------*/ %let _sizechk_=%sysfunc(rxmatch(%sysfunc(rxparse(.|$a|$A|$w)),&n)); %let _propchk_=%sysfunc(rxmatch(%sysfunc(rxparse($a|$A|$w)),&p)); %if (%quote(&seed) eq) or &_sizechk_ or &_propchk_ %then %do; %put &_sizechk_; %if (%quote(&seed) eq) %then %do; %put ==> Error: This is not a valid seed!; %if (%length(&var)) %then %do; &var=.; %end; %else %do; .;%end; %end; %if &_sizechk_ %then %do; %put ==> Error: &n is not a valid sample size!; %if (%length(&var)) %then %do; &var=.; %end; %else %do; .;%end; %end; %if &_propchk_ %then %do; %put ==> Error: &p is not a valid proportion!; %if (%length(&var)) %then %do; &var=.; %end; %else %do; .;%end; %end; %goto finish; %end; %else %if (&n le 1) or (&p ge 1) or (&p le 0) %then %do; %if (&n le 1) %then %do; %put ==> Error: the sample size must be greater than 1!; %if (%length(&var)) %then %do; &var=.; %end; %else %do; .;%end; %end; %if (&p ge 1) or (&p le 0) %then %do; %put ==> Error: the proportion must be greater than 0 and less than 1!; %if (%length(&var)) %then %do; &var=.; %end; %else %do; .;%end; %end; %goto finish; %end; %else %do; %if (%length(&var)) %then %do; %if (not %sysfunc(rxmatch(%sysfunc(rxparse(_|.|$a|$A|$w)),&seed))) %then %do; drop &temp; retain &temp &seed; %let seed=&temp; %end; call ranbin(&seed, &n, &p, &var); %end; %else ranbin(&seed, &n, &p); %end; %finish: %mend _ranbin;